/*
 * @Author: wuqingchun
 * @Date: 2024-03-31 21:33:00
 * @Description: 定时器实现
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-04-01 19:46:38
 */
#ifndef __UTCP_TIMER_H__
#define __UTCP_TIMER_H__

#include <map>
#include <thread>
#include <functional>
#include <mutex>
#include <condition_variable>

using TIMER_TIMER_POINT = std::chrono::time_point<std::chrono::steady_clock>;

class UTCPTimer
{
    struct TimerTask
    {
        TIMER_TIMER_POINT tpTimeout;  // 下次超时时间点 
        //uint64_t Timeout;    // 下次超时时间
        uint32_t Interval;  // 周期性定时任务时间间隔（0表示非周期性定时任务）
        std::function<void (void *)> CallBack;
        void * Arg;
    };
private:
    /* data */
    std::mutex m_objMutex;
    std::condition_variable m_objCondVar;
    //std::map<TIMER_TIMER_POINT, TimerTask> m_mapTasks;
    std::multimap<TIMER_TIMER_POINT, TimerTask> m_mapTasks;

    std::thread m_objThread;
    volatile bool m_bRunning {true};
public:
    UTCPTimer(/* args */);
    ~UTCPTimer();

    int32_t Start();
    
    void Run();

    void AddTask(uint32_t uiTimeout, uint32_t uiInterval, std::function<void (void*)> pfCallback);
};



#endif //__UTCP_TIMER_H__